package com.atguigu.service.impl;

import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@DubboService
public class DictServiceImpl extends BaseServiceImpl<Dict> implements DictService {

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<Dict> getEntityDao() {
        return dictDao;
    }

    @Override
    public List<Map<String, Object>> findZnodes(Long id) {
        //1、以id的值作为parentId查询子节点列表
        List<Dict> dictList = dictDao.findListByParentId(id);
        //2、将第一步查询出来的数据封装成客户端需要的数据(List<Map<String, Object>>),而且map中应有3个键值对
        ArrayList<Map<String, Object>> zNodes = new ArrayList<>();
        for (Dict dict : dictList) {
            Integer count = dictDao.countIsParent(dict.getId());
            //每一个dict对应创建一个map对象
            HashMap<String, Object> map = new HashMap<>();
            //往map中存储id,name,isParent
            map.put("id", dict.getId());
            //判断当前dict是否还有子节点
            map.put("isParent", count > 0 ? true : false);
            map.put("name", dict.getName());
            zNodes.add(map);
        }
        return zNodes;
    }

    @Override
    public List<Dict> findListByParentDictCode(String parentDictCode) {
        return dictDao.findListByParentDictCode(parentDictCode);
    }

    @Override
    public List<Dict> findListByParentId(Long parentId) {
        return dictDao.findListByParentId(parentId);
    }



   /* @Override
    public List<Dict> findListByDictCode(String dictCode) {

        Dict dict = communityDao.findPage();

        return null;
    }*/
}
